﻿Public Class Baremes_CTRL

    Private _cheminFichierMaJ As String = My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData & "\Aniforme\majtarifs\"


    Private _EDM As AniFormEntities
    Private _listeBareme As List(Of Baremes)
    Private _listeCategorie As List(Of String)
    Private _listeCategoriePourAjout As List(Of String)
    Private _indexCourant As Integer
    Private _saisieNouveauVaccin_CTRL As SaisieNouveauVaccin_CTRL



    Public Sub New(ByVal edm As AniFormEntities)
        _EDM = edm
        _listeBareme = New List(Of Baremes)
        _listeCategorie = New List(Of String)
        _listeCategoriePourAjout = New List(Of String)
        _saisieNouveauVaccin_CTRL = New SaisieNouveauVaccin_CTRL(_EDM)
        setListeBareme()
    End Sub

    Public Sub setListeBareme(Optional ByVal cat As String = "Tous")
        Dim liste As New List(Of Baremes)
        Dim requete As IEnumerable(Of Baremes) = (From unBareme In _EDM.Baremes _
                                                  Select unBareme _
                                                  Where unBareme.Archive = False _
                                                  Order By unBareme.CodeGroupement)
        liste = requete.ToList
        Dim requeteCat As IEnumerable(Of String) = (From uneCat In requete _
                                                    Select uneCat.TypeActe _
                                                    Distinct)
        _listeCategorie.Clear()
        _listeCategoriePourAjout.Clear()
        _listeCategorie.Add("Tous")
        _listeCategoriePourAjout.Add("Nouvelle...")
        _listeCategorie.AddRange(requeteCat.ToList)
        _listeCategoriePourAjout.AddRange(requeteCat.ToList)
        If cat <> "Tous" Then
            Dim requeteListeCat As IEnumerable(Of Baremes) = (From unBareme In requete _
                                                               Select unBareme _
                                                               Where unBareme.TypeActe = cat)
            liste = requeteListeCat.ToList
        End If
        _listeBareme.Clear()
        _listeBareme.AddRange(liste)

    End Sub
    Public Property indexCourant() As Integer
        Get
            Return _indexCourant
        End Get
        Set(ByVal value As Integer)
            _indexCourant = value
        End Set
    End Property

    Public Sub SupprimerBareme(ByVal bareme As Baremes)
        bareme.Archive = True
        _EDM.SaveChanges(True)
    End Sub

    Public Sub EnregistrerNouveau(ByVal codeGroupement As String, _
                                  ByVal typeActe As String, _
                                  ByVal libelle As String, _
                                  ByVal isTarifFixe As Boolean, _
                                  ByVal tarifMini As Decimal, _
                                  ByVal tarifMaxi As Decimal, _
                                  Optional ByVal vaccin As Vaccins = Nothing)
        Dim bareme As New Baremes
        With bareme
            If isTarifFixe Then
                .TarifFixe = tarifMini
                .TarifMini = Nothing
                .TarifMaxi = Nothing
            Else
                .TarifFixe = 0
                .TarifMini = tarifMini
                .TarifMaxi = tarifMaxi
            End If
            .CodeGroupement = codeGroupement
            .DateVigueur = Format(Now, "dd/MM/yy")
            .TypeActe = typeActe
            .Libelle = libelle
            .Vaccins = vaccin
            .Archive = False

        End With
        _EDM.AddToBaremes(bareme)
        _EDM.SaveChanges(True)
    End Sub

    Public Sub MaJDepuisXML()
        Dim BaremeFromXML = XDocument.Load("baremes_maj_070730.xml")
        Dim ListeBaremesFromXML As New List(Of Baremes)
        For Each baremeXML In BaremeFromXML.<DocumentElement>.<Baremes>
            Dim bareme As New Baremes
            bareme.CodeGroupement = baremeXML.<CodeGroupement>.Value
            bareme.DateVigueur = baremeXML.<DateVigueur>.Value
            bareme.TypeActe = baremeXML.<TypeActe>.Value
            bareme.Libelle = baremeXML.<Libelle>.Value
            bareme.TarifFixe = Decimal.Parse(baremeXML.<TarifFixe>.Value.Replace(".", ","))
            If bareme.TarifFixe = 0 Then
                bareme.TarifMini = Decimal.Parse(baremeXML.<TarifMini>.Value.Replace(".", ","))
                bareme.TarifMaxi = Decimal.Parse(baremeXML.<TarifMaxi>.Value.Replace(".", ","))
            End If
            Dim requete As IEnumerable(Of Baremes) = From unBareme In _EDM.Baremes _
                                                     Select unBareme _
                                                     Where unBareme.CodeGroupement = bareme.CodeGroupement _
                                                     And unBareme.Archive = False
            If requete.Count <> 0 Then
                If DateDiff(DateInterval.Day, Date.Parse(bareme.DateVigueur), Date.Parse(requete(0).DateVigueur)) <> 0 Then
                    requete(0).Archive = True
                End If
            Else
                _EDM.AddToBaremes(bareme)
            End If
            _EDM.SaveChanges(True)
        Next
    End Sub

    Public ReadOnly Property getListeCategorie(Optional ByVal pourNouveau As Boolean = False) As List(Of String)
        Get
            If pourNouveau Then
                Return _listeCategoriePourAjout
            Else
                Return _listeCategorie
            End If
        End Get
    End Property

    Public ReadOnly Property getVaccinEnregistre() As Vaccins
        Get
            Return _saisieNouveauVaccin_CTRL.getVaccinEnregistre
        End Get
    End Property


    Public ReadOnly Property getListeBareme() As List(Of Baremes)
        Get
            Return _listeBareme
        End Get
    End Property


    Public Function getSaisieNouveauVaccin_CTRL()
        Return _saisieNouveauVaccin_CTRL
    End Function


    Public Sub EnregistrerModif(ByVal bareme As Baremes, ByVal prixMini As Decimal, ByVal prixMaxi As Decimal, ByVal isTarifFixe As Boolean)
        If DateDiff(DateInterval.Day, Date.Parse(bareme.DateVigueur), Now) = 0 Then
            With bareme
                If isTarifFixe Then
                    .TarifFixe = prixMini
                    .TarifMini = Nothing
                    .TarifMaxi = Nothing
                Else
                    .TarifFixe = 0
                    .TarifMini = prixMini
                    .TarifMaxi = prixMaxi

                End If
            End With
        Else
            bareme.Archive = True
            Dim nvoBareme As New Baremes
            With nvoBareme
                .CodeGroupement = bareme.CodeGroupement
                .DateVigueur = Format(Now, "dd/MM/yy")
                .TypeActe = bareme.TypeActe
                .Libelle = bareme.Libelle.Trim
                .Vaccins = bareme.getVaccins
                .Archive = False
                If isTarifFixe Then
                    .TarifFixe = prixMini
                    .TarifMini = Nothing
                    .TarifMaxi = Nothing
                Else
                    .TarifFixe = 0
                    .TarifMini = prixMini
                    .TarifMaxi = prixMaxi
                End If
            End With
            _EDM.AddToBaremes(nvoBareme)
        End If

        _EDM.SaveChanges(True)
    End Sub
End Class
